package jdbctest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author tongchen
 * @create 2022-11-03 16:55
 */
public class JdbcTest1 {
    public static void main(String[] args) throws Exception {
        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2.获取连接
        String url="jdbc:mysql://127.0.0.1:3306/exer?useSSL=false";
        String username="root";
        String passworrd="abc123";
        Connection conn = DriverManager.getConnection(url, username, passworrd);
        //3.定义sql语句
        String sql1="update account set money=2000 where name ='张三'";
        String sql2="update account set money=2000 where name ='李四'";
        //4.获取sql对象
        Statement statement = conn.createStatement();
        try {
            //开启事务
            conn.setAutoCommit(false);
            //5.执行sql语句
            int i = statement.executeUpdate(sql1);
            System.out.println(i);

            int j = statement.executeUpdate(sql2);

            //6.处理返回结果

            System.out.println(j);
            conn.commit();//进行事务的提交
        } catch (Exception e) {
            conn.rollback();//如果出错就进行事务的回滚
        }
        //7.释放资源
        statement.close();
        conn.close();

    }
}
